//
//  SAInventoryWarnVC.m
//  yidiantong
//
//  Created by 黄志刚 on 2020/11/7.
//

#import "SAInventoryWarnVC.h"
#import "SAInventoryDetailCell.h"
#import "SAInventoryWarnHeaderView.h"
#import "SAStockPreviewEarlyWarningListAPI.h"
#import "SAInventoryWarningModel.h"

@interface SAInventoryWarnVC ()<UITableViewDataSource, UITableViewDelegate, HTTPAPIDelegate>

@property (nonatomic, strong) UILabel *stateLab;
@property (nonatomic, strong) UILabel *countLab;
@property (nonatomic, strong) UIButton *stateBtn;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) SAStockPreviewEarlyWarningListAPI *stockPreviewEarlyWarningListAPI;
@property (nonatomic, strong) NSMutableArray<SAInventoryWarningRecords *> *dataMarr;

@property (nonatomic, strong) UIButton *selectTypeBtn;
@property (nonatomic, strong) UIView *moreView;
@property (nonatomic, strong) UIButton *allBtn;
@property (nonatomic, strong) UIView *allLine;
@property (nonatomic, strong) UIButton *pressureBtn;
@property (nonatomic, strong) UIView *pressureLine;
@property (nonatomic, strong) UIButton *lackBtn;
@property (nonatomic, strong) UIButton *selectBtn;
@property (nonatomic, strong) UIView *tipView;
@property (nonatomic, assign) NSInteger currentPage;

@end

@implementation SAInventoryWarnVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

#pragma mark - HTTPAPIDelegate
- (void)fetchData:(id)data fromServerSuccessedWithAPI:(HTTPAPI *)api {
    [self.tableView.mj_header endRefreshing];
    [self.tableView.mj_footer endRefreshing];
    if (self.stockPreviewEarlyWarningListAPI == api) {
        SAInventoryWarningModel *model = [SAInventoryWarningModel mj_objectWithKeyValues:data[@"data"]];
        if (self.currentPage == 1) {
            if (self.dataMarr.count > 0){
                 [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];
            }
            self.dataMarr = model.records;
            self.countLab.text = [NSString stringWithFormat:@"共%@款",model.obj.productCount];
        }else {
            for (SAInventoryWarningRecords *model2 in model.records) {
                [self.dataMarr addObject:model2];
            }
        }
        self.tipView.hidden = (self.dataMarr.count > 0);
        [self.tableView reloadData];
    }
}

- (void)fetchDataFromServerFailedWithError:(NSError *)error API:(HTTPAPI *)api {
    [self.tableView.mj_header endRefreshing];
    [self.tableView.mj_footer endRefreshing];
}


#pragma mark - UITableView Delegate & DataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataMarr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    SAInventoryWarningRecords *model = self.dataMarr[section];
    return model.skuList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SAInventoryDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SAInventoryDetailCell" forIndexPath:indexPath];
    SAInventoryWarningRecords *model = self.dataMarr[indexPath.section];
    cell.warnModel = model.skuList[indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return kScaleNum(35);
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    SAInventoryWarnHeaderView *headerView = [[SAInventoryWarnHeaderView alloc] initWithReuseIdentifier:@"SAInventoryWarnHeaderView"];
    SAInventoryWarningRecords *model = self.dataMarr[section];
    headerView.model = model;
    return headerView;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectZero];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return kScaleNum(74);
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return kScaleNum(0.001);
}

#pragma mark - event
-(void)clickedStateBtn {
    self.moreView.hidden = NO;
}

-(void)removeView {
    self.moreView.hidden = YES;
}

-(void)clickedAllBtn:(UIButton *)sender {
    self.moreView.hidden = YES;
    self.selectBtn.selected  = NO;
    self.selectBtn = sender;
    self.selectBtn.selected  = YES;
    self.stockPreviewEarlyWarningListAPI.type = @"";
    self.stateLab.text = @"全部";
    self.currentPage = 1;
    [self loadData];
}

-(void)clickedPressureBtn:(UIButton *)sender {
    self.moreView.hidden = YES;
    self.selectBtn.selected  = NO;
    self.selectBtn = sender;
    self.selectBtn.selected  = YES;
    self.stockPreviewEarlyWarningListAPI.type = @"1";
    self.stateLab.text = @"压货";
    self.currentPage = 1;
    [self loadData];
}

-(void)clickedLackBtn:(UIButton *)sender {
    self.moreView.hidden = YES;
    self.selectBtn.selected  = NO;
    self.selectBtn = sender;
    self.selectBtn.selected  = YES;
    self.stockPreviewEarlyWarningListAPI.type = @"2";
    self.stateLab.text = @"缺货";
    self.currentPage = 1;
    [self loadData];
}

#pragma mark - initUI
-(void)initUI {
    [self setNavigationItemBackBBIAndTitle:@"库存预警"];
    [self setBgImage:nil];
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:self.stateLab];
    [self.view addSubview:self.countLab];
    [self.view addSubview:self.stateBtn];
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.tipView];
    [self.view addSubview:self.moreView];
    
    self.selectBtn = self.allBtn;
    
    self.currentPage = 1;
    self.stockPreviewEarlyWarningListAPI.currentPage = self.currentPage;
    self.stockPreviewEarlyWarningListAPI.pageSize = 20;
    [self.stockPreviewEarlyWarningListAPI sendRequestToServerWithActivityViewAndMask:GET];
    
    __weak typeof(self) weakSelf = self;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf.view endEditing:YES];
        weakSelf.currentPage = 1;
        [weakSelf loadData];
    }];
    MJRefreshAutoStateFooter *mj_footer = [MJRefreshAutoStateFooter footerWithRefreshingBlock:^{
        [weakSelf.view endEditing:YES];
        weakSelf.currentPage ++;
        [weakSelf loadData];
    }];
    [mj_footer setTitle:@"已经到底啦~" forState:MJRefreshStateIdle];//设置闲置状态下不显示“点击或上拉加载更多”
    self.tableView.mj_footer = mj_footer;
    
    [self loadData];
}

-(void)loadData {
    self.stockPreviewEarlyWarningListAPI.currentPage = self.currentPage;
    [self.stockPreviewEarlyWarningListAPI sendRequestToServerWithActivityViewAndMask:GET];
}

#pragma mark - lazy
-(UILabel *)stateLab {
    if (!_stateLab) {
        _stateLab = [[UILabel alloc] initWithFrame:SARectMake(30, StatusHeight, 50, 34)];
        _stateLab.text = @"全部";
        _stateLab.font = kFont(15);
        _stateLab.textColor = kBlackColor;
        _stateLab.textAlignment = NSTextAlignmentLeft;
    }
    return _stateLab;
}

-(UILabel *)countLab {
    if (!_countLab) {
        _countLab = [[UILabel alloc] initWithFrame:SARectMake(kBaseScreenWidth - 230, StatusHeight, 200, 34)];
        _countLab.font = kFont(15);
        _countLab.textColor = kGrayColor;
        _countLab.textAlignment = NSTextAlignmentRight;
    }
    return _countLab;
}

-(UIButton *)stateBtn {
    if (!_stateBtn) {
        _stateBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_stateBtn setImage:[UIImage imageNamed:@"allow_bule_down"] forState:UIControlStateNormal];
        _stateBtn.frame = SARectMake(30, StatusHeight, 60, 34);
        _stateBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        [_stateBtn addTarget:self action:@selector(clickedStateBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _stateBtn;
}

-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kScaleNum(StatusHeight + 34), kScreenWidth, kScreenHeight - kScaleNum(StatusHeight + 34)) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = kLineColor;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsHorizontalScrollIndicator = NO;
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }else {
            self.automaticallyAdjustsScrollViewInsets = NO;
        }
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        [_tableView registerClass:[SAInventoryDetailCell class] forCellReuseIdentifier:@"SAInventoryDetailCell"];
        [_tableView registerClass:[SAInventoryWarnHeaderView class] forHeaderFooterViewReuseIdentifier:@"SAInventoryWarnHeaderView"];
    }
    return _tableView;
}

-(SAStockPreviewEarlyWarningListAPI *)stockPreviewEarlyWarningListAPI {
    if (!_stockPreviewEarlyWarningListAPI) {
        _stockPreviewEarlyWarningListAPI = [SAStockPreviewEarlyWarningListAPI apiWithDelegate:self];
    }
    return _stockPreviewEarlyWarningListAPI;
}

-(NSMutableArray<SAInventoryWarningRecords *> *)dataMarr {
    if (!_dataMarr) {
        _dataMarr = [NSMutableArray array];
    }
    return _dataMarr;
}

-(UIView *)moreView {
    if (!_moreView) {
        _moreView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
        _moreView.backgroundColor = kColor(0, 0, 0, 0.3);
        _moreView.hidden = YES;
        _moreView.userInteractionEnabled = YES;
        
        UIImageView *imageV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Customer_moreView"]];
        imageV.frame = SARectMake(13, StatusHeight + 35, 88, 97);
        [_moreView addSubview:imageV];
        [imageV addSubview:self.allBtn];
        [imageV addSubview:self.allLine];
        [imageV addSubview:self.pressureBtn];
        [imageV addSubview:self.pressureLine];
        [imageV addSubview:self.lackBtn];
        imageV.userInteractionEnabled = YES;
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(removeView)];
        [_moreView addGestureRecognizer:tap];
    }
    return _moreView;
}

-(UIButton *)allBtn {
    if (!_allBtn) {
        _allBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _allBtn.frame = SARectMake(0, 5, 88, 30);
        [_allBtn setTitle:@"全部" forState:UIControlStateNormal];
        _allBtn.titleLabel.font = kFont(14);
        [_allBtn setTitleColor:kBlackColor forState:UIControlStateNormal];
        [_allBtn setTitleColor:kBuleColor forState:UIControlStateSelected];
        [_allBtn addTarget:self action:@selector(clickedAllBtn:) forControlEvents:UIControlEventTouchUpInside];
        _allBtn.tag = 1;
        _allBtn.selected = YES;
    }
    return _allBtn;
}

-(UIView *)allLine {
    if (!_allLine) {
        _allLine = [[UIView alloc] initWithFrame:SARectMake(4, 35, 80, 1)];
        _allLine.backgroundColor = kLineColor;
    }
    return _allLine;
}

-(UIButton *)pressureBtn {
    if (!_pressureBtn) {
        _pressureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _pressureBtn.frame = SARectMake(0, 36, 88, 30);
        [_pressureBtn setTitle:@"压货" forState:UIControlStateNormal];
        _pressureBtn.titleLabel.font = kFont(14);
        [_pressureBtn setTitleColor:kBlackColor forState:UIControlStateNormal];
        [_pressureBtn setTitleColor:kBuleColor forState:UIControlStateSelected];
        [_pressureBtn addTarget:self action:@selector(clickedPressureBtn:) forControlEvents:UIControlEventTouchUpInside];
        _pressureBtn.tag = 2;
    }
    return _pressureBtn;
}

-(UIView *)pressureLine {
    if (!_pressureLine) {
        _pressureLine = [[UIView alloc] initWithFrame:SARectMake(4, 66, 80, 1)];
        _pressureLine.backgroundColor = kLineColor;
    }
    return _pressureLine;
}

-(UIButton *)lackBtn {
    if (!_lackBtn) {
        _lackBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _lackBtn.frame = SARectMake(0, 67, 88, 30);
        [_lackBtn setTitle:@"缺货" forState:UIControlStateNormal];
        _lackBtn.titleLabel.font = kFont(14);
        [_lackBtn setTitleColor:kBlackColor forState:UIControlStateNormal];
        [_lackBtn setTitleColor:kBuleColor forState:UIControlStateSelected];
        [_lackBtn addTarget:self action:@selector(clickedLackBtn:) forControlEvents:UIControlEventTouchUpInside];
        _lackBtn.tag = 3;
    }
    return _lackBtn;
}

-(UIView *)tipView {
    if (!_tipView) {
        _tipView = [[UIView alloc] initWithFrame:CGRectMake(0, kScaleNum(StatusHeight + 34), kScreenWidth, kScreenHeight - kScaleNum(StatusHeight + 34))];
        _tipView.backgroundColor = kLineColor;
        UIImageView *icon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Shop_search2"]];
        icon.frame = SARectMake(124, 147 - StatusHeight, 138, 83);
        [_tipView addSubview:icon];
        UILabel *titleLab = [[UILabel alloc] initWithFrame:SARectMake(0, 240 - StatusHeight, kBaseScreenWidth, 30)];
        titleLab.text = @"暂无相关信息";
        titleLab.textAlignment = NSTextAlignmentCenter;
        titleLab.textColor = kGrayColor;
        [_tipView addSubview:titleLab];
    }
    return _tipView;
}

@end
